-module(cdr_event).
-behaviour(gen_event).
-export([start_link/0, init/1,handle_event/2,handle_call/2,handle_info/2,terminate/2,code_change/3]).
-define(SERVER,?MODULE).
-record(state,{}).

start_link() ->
	{ok,Pid} = gen_event:start_link({local,?SERVER}),
	gen_event:add_handler(?SERVER,?MODULE,[]),
	{ok,Pid}.

init([]) ->
	io:format("register the event"),
	{ok,#state{}}.
handle_event({cdr,Sub},State) ->
	Cdr = cdr_to_json:do(Sub),
	push_server:push({evt,Cdr}),
  CVS = cdr_to_cvs:do(Sub),
	cdr_saver:do({cdr,CVS}),
	{ok,State};
handle_event(Event,State) ->
	io:format("recevied the event ~p~n",[Event]),
	{ok,State}.
handle_call(_Request,State) ->
	{ok,ok,State}.
handle_info(_Info,State) ->
	{ok,State}.
terminate(Reason,_State) ->
	io:format("terminte with ~p ~n",[Reason]),
	ok.
code_change(_, State, _) ->
	{ok,State}.
